package com.letu.core.excel.export;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import javax.annotation.Nullable;
import java.io.File;
import java.io.OutputStream;
import java.util.Objects;

/**
 * @ClassName ExcelExport
 * @Description TODO
 * @Author yuwx
 * @Date 2021/11/3 10:17
 * @Version 1.0
 **/
public class ExcelExport {


    public static void export(File file, @Nullable Iterable<String> header, @Nullable Iterable<Object> data) {
        if (Objects.isNull(file)) {
            throw new NullPointerException("file is null");
        }
        ExcelWriter excelWriter = ExcelUtil.getWriter(file);
        if (!CollectionUtil.isEmpty(header)) {
            excelWriter.writeHeadRow(header);
        }
        if (!CollectionUtil.isEmpty(data)) {
            excelWriter.write(data);
        }
        excelWriter.flush();
    }

    public static void export(OutputStream outputStream, @Nullable Iterable<String> header, @Nullable Iterable<Object> data) {
        if (Objects.isNull(outputStream)) {
            throw new NullPointerException("outputStream is null");
        }
        ExcelWriter excelWriter = ExcelUtil.getWriter(true);
        if (!CollectionUtil.isEmpty(header)) {
            excelWriter.writeHeadRow(header);
        }
        if (!CollectionUtil.isEmpty(data)) {
            excelWriter.write(data);
        }
        excelWriter.flush(outputStream);
    }
}
